/*
 * $Id$
 *
 * Copyright (c) 1998-2008 John Morrison.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


/*
 * SkinChooserDialog.java
 *
 * Created on 30 June 2004, 21:30
 */

package com.jbolo.gui.dialog;

import java.io.*;
import com.jbolo.gui.INIFile;
import com.jbolo.gui.Skin;
import com.jbolo.gui.BoloPreferences;

/**
 *
 * @author  John Morrison
 */
public class SkinChooserDialog extends java.awt.Dialog implements java.awt.event.ItemListener {
    /** No Skin in use special name */
    private static final String NO_SKIN_NAME = "No Skin (Default)";
    /** Default words if ini file items are missing */
    private static final String DEFAULT_COMMENT = "N/A";
    
    /** Path to skin files */
    private String path;
    /** Currently selected skin */
    private String currentSkin;
    /** Previous skin loaded */
    private String prevSkin;
    
    /** Creates new form SkinChooserDialog 
     *
     * @param parent Parent of the frame
     * @param model Is this frame model
     */
    public SkinChooserDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        setDefaultInformation();
        setSize(450, 350);
        prevSkin = Skin.GetInstance().getFileName();
        currentSkin = null;
        list1.add(NO_SKIN_NAME);
        //FIXME
        path = "c:/winbolo/skins/";
        File f = new File(path);
        File files[] = f.listFiles();
        int count = 0;
        String name;
        if (files != null) {
            while (count < files.length) {
                name = files[count].getName();
                if (isSkinFileNameEnding(name) == true) {
                    list1.add(name.substring(0, name.length()-4));
                }
                count++;
            }
        }
        list1.addItemListener(this);
        list1.select(0);
    }
    
    /**
     * Does the filename match the file
     *
     * @param name Filename
     * @return boolean
     */
    private boolean isSkinFileNameEnding(String name) {
        name = name.toLowerCase();
        if (name.endsWith(".zip") == true || name.endsWith(".wsf") == true) {
            return true;
        }
        return false;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        list1 = new java.awt.List();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        label5 = new java.awt.Label();
        lblName = new java.awt.Label();
        lblAuthor = new java.awt.Label();
        lblNotes = new java.awt.Label();
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();

        setLayout(null);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        add(list1);
        list1.setBounds(30, 50, 140, 190);

        label1.setText("Information");
        add(label1);
        label1.setBounds(190, 50, 160, 20);

        label2.setText("Name:");
        add(label2);
        label2.setBounds(190, 80, 41, 20);

        label3.setText("Author:");
        add(label3);
        label3.setBounds(190, 100, 42, 20);

        label4.setText("Notes:");
        add(label4);
        label4.setBounds(190, 120, 40, 20);

        label5.setText("If not all skin items are present the default skin/sound items are used.");
        add(label5);
        label5.setBounds(30, 270, 390, 20);

        add(lblName);
        lblName.setBounds(230, 80, 190, 20);

        add(lblAuthor);
        lblAuthor.setBounds(230, 100, 190, 20);

        lblNotes.setText("label6");
        add(lblNotes);
        lblNotes.setBounds(230, 120, 190, 130);

        button1.setLabel("OK");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        add(button1);
        button1.setBounds(150, 300, 60, 24);

        button2.setLabel("Cancel");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        add(button2);
        button2.setBounds(230, 300, 55, 24);

        pack();
    }//GEN-END:initComponents

    /**
     * Cancel button pressed
     *
     * @param evt Action event
     */
    private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
        // TODO add your handling code here:
        if (prevSkin != null) {
            Skin.GetInstance().loadSkin(prevSkin);
        }
        BoloPreferences.GetInstance().writePriveteProfileString("GAME OPTIONS", "SKIN", prevSkin);
    }//GEN-LAST:event_button2ActionPerformed

    /**
     * OK Button is pressed
     *
     * @param evt Event
     */
    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        // TODO add your handling code here:
        if (currentSkin != null) {
            Skin.GetInstance().loadSkin(currentSkin);
        }
        BoloPreferences.GetInstance().writePriveteProfileString("GAME OPTIONS", "SKIN", currentSkin);
    }//GEN-LAST:event_button1ActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new SkinChooserDialog(new java.awt.Frame(), true).show();
    }
    
    /**
     * Sets the default information in the dialog box
     */
    private void setDefaultInformation() {
        lblAuthor.setText("John Morrison");
        lblName.setText(NO_SKIN_NAME);
        lblNotes.setText("None");        
    }
    
    /**
     * Sets the skin informaion in the dialog
     *
     * @param ini INI File from inside the skin
     */
    private void setInformation(INIFile ini) {
        lblAuthor.setText(DEFAULT_COMMENT);
        lblName.setText(list1.getSelectedItem());
        lblNotes.setText(DEFAULT_COMMENT);
        if (ini != null) {
            lblAuthor.setText(ini.getPrivateProfileString("SKIN", "Author", DEFAULT_COMMENT));
            lblNotes.setText(ini.getPrivateProfileString("SKIN", "Notes", DEFAULT_COMMENT));
        }
    }
    
    /**
     * A different skin has been selected in the list
     *
     * @param e Item Event that occured
     */
    public void itemStateChanged(java.awt.event.ItemEvent e) {
        Skin skin = Skin.GetInstance();
        
        if (list1.getSelectedItem().equals(NO_SKIN_NAME) == true) {
            // No skin selected
            skin.unload();
            setDefaultInformation();
            currentSkin = null;
        } else if (skin.loadSkin(path + list1.getSelectedItem() + ".zip") == true) {
            setInformation(skin.getIniFile());
            currentSkin = skin.getFileName();
        } else if (skin.loadSkin(path + list1.getSelectedItem() + ".wsf") == true) {
            setInformation(skin.getIniFile());
            currentSkin = skin.getFileName();
        } else {
            new MessageBox(this, "Unable to load Skin File");
        }        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private java.awt.Button button2;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.Label label5;
    private java.awt.Label lblAuthor;
    private java.awt.Label lblName;
    private java.awt.Label lblNotes;
    private java.awt.List list1;
    // End of variables declaration//GEN-END:variables

}
